/*
 * Copyright (C) 2019
 * <tang-haifeng@foxmail.com> <pengren.mcu@qq.com>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 *
 */

#include <common.h>
#include <malloc.h>
#include <clk.h>
#include <led.h>
#include <pwm.h>
#include <pci.h>
#include <net.h>
#include <power/regulator.h>

#include <asm/io.h>
#include <asm/gpio.h>

#include <mach/ls2k.h>

#include "../common/platform.h"

DECLARE_GLOBAL_DATA_PTR;

#ifdef CONFIG_I2C_EEPROM
#define LS_BOARD_MAXLAN 32
#define LS_BOARD_ID_LAN 20
#define LS_BOARD_SN_LAN 22

int get_eeprom_info(void)
{
	uchar *datap = malloc(LS_BOARD_MAXLAN);
	int ret;

	memset(datap, 0, LS_BOARD_MAXLAN);

	ret = eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, datap, LS_BOARD_ID_LAN);
	if (ret) {
		printf("Cannot read I2C EEPROM.\n");
		goto err;
	}

	if (strncmp((const char *)datap + 3, "LSGD", 4) == 0) {
		printf("Board: %s\n", datap);
		//get SN
		memset(datap, 0, LS_BOARD_MAXLAN);
		eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR, 32, datap, LS_BOARD_SN_LAN);
		printf("SN: %s\n", datap);
		//get mac addr 0
		memset(datap, 0, LS_BOARD_MAXLAN);
		eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR, 64, datap, 16);
		if (is_valid_ethaddr(datap))
			eth_env_set_enetaddr("ethaddr", datap);
		//get mac addr 1
		memset(datap, 0, LS_BOARD_MAXLAN);
		eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR, 64 + 16, datap, 16);
		if (is_valid_ethaddr(datap))
			eth_env_set_enetaddr("eth1addr", datap);

	} else {
		goto err;
	}

	return 0;

err:
	printf("Board: %s\n", CONFIG_LOONGSON_BOARD_NAME);
	return -1;
}
#endif //CONFIG_I2C_EEPROM

#ifdef CONFIG_NAND_LS1X
static void ls2x_set_nand(void)
{
	u32 val = 0;
	void __iomem *mux_base = ioremap(LS2X_MUX_BASE, 1);

	/* 设置nand gpio复用 nand_sel=1 */
	val = readl(mux_base);
	val = val | (1<<9);
	writel(val, mux_base);
}
#endif

#ifndef NAND_LS2K_DMACHAN
#define NAND_LS2K_DMACHAN 0
#endif
#ifndef SDIO_LS2K_DMACHAN
#define SDIO_LS2K_DMACHAN 4
#endif

/* 设置设备使用的apb dma通道 */
static void ls2x_set_apbdma(void)
{
	u32 val;
	void __iomem *apb_dma_cfg = ioremap(LS2X_APB_DMA_CFG, 1);

	val = (NAND_LS2K_DMACHAN<<0) |
		(1<<3) | (2<<6) | (1<<9) | (2<<12) | (SDIO_LS2K_DMACHAN<<15) | (1<<18) | (3<<21);
	__raw_writel(val, apb_dma_cfg);
}

int board_early_init_f(void)
{
	ls2x_set_apbdma();
#ifdef CONFIG_NAND_LS1X
	ls2x_set_nand();
#endif
	return 0;
}

int ls2k_board_late_init(void)
{
#ifdef BEEP_GPIO
	{
	int ret;
	ret = gpio_request(BEEP_GPIO, "beep");
	if (ret == 0) {
		int i;
		for (i=0; i<500; i++) {
			gpio_direction_output(BEEP_GPIO, i & 0x1);
			udelay(220);
		}
		gpio_direction_output(BEEP_GPIO, 0);
	}
	}
#endif

	//usb hub reset
	{
	int ret;
	//i2c扩展gpio 芯片的P03脚
	ret = gpio_request(73, "usb-hub-reset");
	if (ret == 0) {
		gpio_direction_output(73, 0);
		udelay(500);
		gpio_direction_output(73, 1);
	}
	}

#ifdef CONFIG_DM_REGULATOR
	{
	struct udevice *dev;

	if (regulator_get_by_platname("vdd-fan", &dev) == 0) {
		regulator_set_value(dev, 12000000);
//		regulators_enable_boot_on(false);
	}
	}
#endif
#ifdef CONFIG_PWM_LOONGSON
#ifndef CONFIG_DM_PWM
	pwm_init(3, 0, 1);
	pwm_config(3, 100000, 7812500);
	pwm_enable(3);
#endif
#endif
	return 0;
}
